// threadtest.cc
//	implement some test functions
//
// added by tangyafeng@seedclass 3-26-2011

#include "threadtest.h"
#include "filesys.h"
#include "scheduler.h"


//----------------------------------------------------------------------
// ClearString
// Clear a char array, this is used for clear the temp char array
//----------------------------------------------------------------------
void ClearString(char* str)
{
    int index = 0;
    while (*(str + index) != '\0') *(str + index++) = '\0';
    return;
}

//----------------------------------------------------------------------
// ReadThreadTestPara
//      Read parameters from test file, default file "TestThread"
//
//      added by tangyafeng1990@seedclass 2-23-2011
//----------------------------------------------------------------------
bool
ReadThreadTestPara(Scheduler *scheduler, char* FileName, ThreadTestStruct* tts) {
    int numofthread;
    char ctmp[256]; // There must be a better solution...OTZ...

    FileSystem filesystem;
    OpenFile *openfile = NULL;

    SchedPolicy fcfs = FCFS;
    SchedPolicy hpfs = HPFS;

    if (!(openfile = filesystem.Open(FileName)))
    {
        return false;
    }

    //Read Schduler Policy       
    char tmp;
    int index = 0;
    int position = 0;
    for (; tmp != '\n' && position < openfile->Length(); position++)
    {
        openfile->ReadAt(&tmp , 1, position);
        *(ctmp + index++) = tmp;
    }
    ctmp[index - 1] = '\0';

    if (!strcmp(ctmp, "FCFS"))
    {
        tts->SchedPolicy = 0;
        scheduler->SetSchedPolicy(fcfs);
    }
    else if (!strcmp(ctmp, "HPFS"))
    {
        tts->SchedPolicy = 1;
        scheduler->SetSchedPolicy(hpfs);
    }
    else
    {
        tts->SchedPolicy = 0;
        scheduler->SetSchedPolicy(fcfs);
    }

    index = 0;
    tmp = '\0';
    ClearString(ctmp);
    // Read the Number of Thread that will be created
    for (; tmp != '\n' && position < openfile->Length(); position++)
    {
        openfile->ReadAt(&tmp, 1, position);
        *(ctmp + index++) = tmp;
    }
    ctmp[index - 1] = '\0';
    numofthread = atoi(ctmp);
    tts->NumOfThread = numofthread;

    // allocate space for test infomation
    tts->CTTS = (ChildThreadTestStruct*)malloc(numofthread * sizeof(ChildThreadTestStruct));

    ClearString(ctmp);
    for (int i = 0; i < numofthread; i++)
    {
        index =0;
        for (; tmp != ' ' && position < openfile->Length(); position++)
        {
            openfile->ReadAt(&tmp, 1, position);
            *(tts->CTTS[i].ThreadName + index++) = tmp;
        }
        tts->CTTS[i].ThreadName[index - 1] = '\0';

        index = 0;
        tmp = '\0';
        for (; tmp != ' ' && position < openfile->Length(); position++)
        {
            openfile->ReadAt(&tmp, 1, position);
            *(ctmp + index++) = tmp;
        }
        ctmp[index - 1] = '\0';
        tts->CTTS[i].ThreadPriority = atoi(ctmp);

        index = 0;
        tmp = '\0';
        ClearString(ctmp);
        for (; tmp != '\n' && position < openfile->Length(); position++)
        {
             openfile->ReadAt(&tmp, 1, position);
             *(ctmp + index++) = tmp;
        }
        ctmp[index - 1] = '\0';
        tts->CTTS[i].RemainRunTime = atoi(ctmp);
    }

    return true;
}


